package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.SentiveMapper;
import com.heima.wemedia.service.SensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-11-07 15:27
 */
@Service
@Slf4j
public class SensitiveServiceImpl extends ServiceImpl<SentiveMapper, WmSensitive>
        implements SensitiveService {


    @Override
    public ResponseResult selectList(SensitiveDto dto) {
        Page<WmSensitive> page = new Page<>(dto.getPage(),dto.getSize());
        page.setSize(dto.getSize());
        page.setCurrent(dto.getPage());

//        QueryWrapper<WmSensitive> wrapper = new QueryWrapper<>();
//        wrapper.like("sensitives",dto.getName()); // 冰  where sensitives like '%冰%'

        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like(WmSensitive::getSensitives,dto.getName());
        }
        //链式编程简化
        Page<WmSensitive> resultPage = page(page, wrapper);

//        Page<WmSensitive> resultPage = sensitiveService.page(new Page<>(dto.getPage(),dto.getSize()),
//                Wrappers.<WmSensitive>lambdaQuery().
//                like(StringUtils.isNotBlank(dto.getName()),WmSensitive::getSensitives,dto.getName()));

        //以结果为主
        PageResponseResult result = new PageResponseResult((int) resultPage.getCurrent(),
                (int) resultPage.getSize(),
                (int) resultPage.getTotal());

        result.setData(resultPage.getRecords()); //很多同学犯错 忘记加上getRecords

        return result;
    }
}
